package xin.marcher.module.search.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.search.api.CompletionSearchWordApi;
import xin.marcher.module.search.domain.dto.CompletionSearchWordDTO;
import xin.marcher.module.search.domain.dto.CompletionSearchWordResultDTO;
import xin.marcher.module.search.domain.request.CompletionSearchWordRequest;
import xin.marcher.module.search.domain.request.QueryCompletionSearchWordPageRequest;
import xin.marcher.module.search.service.CompletionSearchWordService;

/**
 * 搜索词
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = CompletionSearchWordApi.class, retries = 0)
public class CompletionSearchWordApiImpl implements CompletionSearchWordApi {

    @Autowired
    private CompletionSearchWordService completionSearchWordService;

    /**
     * 保存补全词接口
     */
    @Override
    public JsonResult<CompletionSearchWordResultDTO> saveCompletionSearchWord(CompletionSearchWordRequest request) {
        try {
            CompletionSearchWordResultDTO resultDTO = completionSearchWordService.saveCompletionSearchWord(request);
            return JsonResult.buildSuccess(resultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询补全词接口
     */
    @Override
    public JsonResult<PageResult<CompletionSearchWordDTO>> listCompletionSearchWordPage(QueryCompletionSearchWordPageRequest request) {
        try {
            PageResult<CompletionSearchWordDTO> resultDTO = completionSearchWordService.listCompletionSearchWordPage(request);
            return JsonResult.buildSuccess(resultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
